package com.itheima.controller;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemICheckGroupIdService;
import com.itheima.service.CheckitemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/*检查相关
 *
 * */
@Slf4j
@RestController
@RequestMapping("checkitem")
public class CheckitemController {

    @Autowired
    private CheckitemService checkitemService;

    @Autowired
    private CheckItemICheckGroupIdService checkItemICheckGroupIdService;

    /*
     * 新增检查组
     * */
    @PostMapping("add")
    public Result add(@RequestBody CheckItem checkItem) {
        log.info("新增检查项 {}" , checkItem);

        //新增检查组方法
        checkitemService.add(checkItem);

        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS, null);
    }

    //删除检查组
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        log.info("删除检查项 {}" , id);

        //删除检查组方法
        checkitemService.delete(id);

        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS, null);
    }

    //分页查询检查项
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPage) {
        log.info("分页查询检查项{}" , queryPage);

        //分页查询检查项方法
        PageResult pageResult = checkitemService.findPage(queryPage);

        return pageResult;
    }

    //查询所有检查项
    @GetMapping("/findAll")
    public Result findAll() {
        log.info("查询所有检查项");

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemService.findAll());
    }

    //根据id查询检查项
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        log.info("根据id查询检查项 {}" , id);

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemService.findById(id));
    }

    //编辑检查项
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        log.info("编辑检查项 {}" , checkItem);

        checkitemService.edit(checkItem);

        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, null);
    }

    //根据检查组id查询关联的检查项id
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer checkgroupId) {
        log.info("根据检查组id查询关联的检查项id {}", checkgroupId);

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemICheckGroupIdService.findCheckItemIdsByCheckGroupId(checkgroupId));
    }


}
